package xyz.xiangyushui.dao.user.member;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import xyz.xiangyushui.dao.Dao;
import xyz.xiangyushui.javaBean.salesPlan.DailyPlanCommodity;

public class CommodityReservableDao extends Dao{
	
	public List<DailyPlanCommodity> getCommodityReservable(String planId){
		List<DailyPlanCommodity> commodities=new LinkedList<>();
		List<Object> list=db.excuteQuery("select commodityIdName,stock,price from commodityReservable where planId=? ", new Object[]{planId});
		if(!list.isEmpty()){
			for(Object o:list){
				@SuppressWarnings("unchecked")
				Map<String,Object> m=(Map<String, Object>) o;
				DailyPlanCommodity dpc=new DailyPlanCommodity();
				dpc.setCommodityIdName((String) m.get("commodityIdName"));
				dpc.setNum((int) m.get("stock"));
				dpc.setPrice((double) m.get("price"));
				commodities.add(dpc);
			}
		}
		return commodities;
	}
	
	public void updateStock(int num,String planId,String commodityIdName){
		db.executeUpdate("update commodityReservable set stock=stock+? where planId=? and commodityIdName=?", new Object[]{num,planId,commodityIdName});
	}
	
	public int getStock(String planId,String commodityIdName){
		List<Object> list=db.excuteQuery("select stock from commodityReservable where planId=? and commodityIdName=? ", new Object[]{planId,commodityIdName});
		if(!list.isEmpty()){
			@SuppressWarnings("unchecked")
			Map<String,Object> m=(Map<String, Object>) list.get(0);
			return (int) m.get("stock");
		}
		return 0;
	}
}
